package djboom.materials.display.elements {
	import flash.geom.Rectangle;

	import org.casalib.display.CasaBitmap;
	import org.casalib.util.DrawUtil;

	import flash.display.BitmapData;
	import flash.events.Event;

	/**
	 * .
	 * <br>
	 * 
	 * @author DJ BooM 
	 * @since 02.07.2010
	 * 
	 */

	public class HotSpot extends SimpleSmartSprite {

		// ----------------------------------------------------------------------------
		// Свойства класса.
		// ----------------------------------------------------------------------------

		private var _bitmap:CasaBitmap;

		// ----------------------------------------------------------------------------
		// Конструктор.
		// ----------------------------------------------------------------------------

		public function HotSpot() {
			super();
		}	 

		// ----------------------------------------------------------------------------
		// Публичные методы.
		// ----------------------------------------------------------------------------

		public function draw(points:Array, alpha:Number = 0, color:uint = 0x00ff33):void {
			graphics.beginFill(color, alpha);
			DrawUtil.drawShape(graphics, points);
			graphics.endFill();
		}

		public function drawRect(rect:Rectangle, alpha:Number = 0, color:uint = 0x00ff33):void {
			graphics.beginFill(color, alpha);
			graphics.drawRect(rect.x, rect.y, rect.width, rect.height);
			graphics.endFill();
		}

		public function clear():void {
			graphics.clear();
		}

		public function drawBitmap(bitmapData:BitmapData, pixelSnapping:String = "auto", smoothing:Boolean = false):void {
			if (_bitmap == null) {
				_bitmap = new CasaBitmap(bitmapData, pixelSnapping, smoothing);
				addChild(_bitmap);
			} else {
				_bitmap.bitmapData = bitmapData;
			}
		}

		public function clearBitmap():void {
			var bmd:BitmapData = _bitmap.bitmapData;
			bmd.lock(); 
			bmd.fillRect(bmd.rect, 0); 
			bmd.unlock();
		}

		override public function clearContent(event:Event):void {
			super.clearContent(event);
			
			graphics.clear();
			
			if (_bitmap != null) {
				_bitmap.destroy();
				_bitmap = null;
			}
		}

		override public function destroy():void {
			if (_bitmap != null) {
				_bitmap.destroy();
				_bitmap = null;
			}
			
			graphics.clear();
			
			super.destroy();
		}
	}
}
